public class IntStack {

    private int stackSize;
    private int stackPointer;
    private int[] element;

    IntStack(int size)   //Constructor
    {
        stackSize = size;
        element = new int[stackSize];
        stackPointer=-1;
    }

    public boolean isEmpty(){

        return (stackPointer==-1);
    }

    public boolean isFull(){

        return (stackPointer==stackSize-1);
    }

    public int pop() throws StackException {
        if(isEmpty())
            throw new StackException("stack is Empty.");
        else
            return element[stackPointer--];
    }

    public void push(int newElement) throws StackException {
        if(isFull())
            throw new StackException("Stack is full");

        else
            element[++stackPointer] = newElement;

    }

    public int getElement(int index){
        return element[index];
    }
    
    public int top() {
        return element[stackPointer];
    }
    
    public int numberOfItems() {
        return stackPointer+1;
    }
    
    public void clearStack() {
        while(stackPointer>-1){
            pop();
        }
    }
    
    public boolean searchStack(int input) {
        int i=stackPointer;
        while(i>-1){
            if (element[i]==input)
                return true;
                i--;
        }   
         return false;
    }

    public String printStack() {
        String s="";
        int i=stackPointer;
        while(i>-1){
            s+=element[i]+"\n";
            i--;
        }
        return s;
    }
}
